package com.atguigu.tingshu.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;

    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient;

    @Autowired
    private KafkaService kafkaService;

    @Override
    public void updatePaymentStatus(Transaction result) {
        //  payment_status
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        //  获取微信订单号
        paymentInfo.setOutTradeNo(result.getTransactionId());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(result));
        //  设置更新条件
        LambdaUpdateWrapper<PaymentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo,result.getOutTradeNo());
        //  第一个参数：要修改的内容；第二个参数：条件
        this.update(paymentInfo,wrapper);

        //  查询paymentInfo;
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(wrapper);
        String topic = paymentInfoQuery.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)? KafkaConstant.QUEUE_ORDER_PAY_SUCCESS:KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS;
        //  发送消息修改充值或订单业务记录信息。谁监听：订单业务-order; 充值-account
        kafkaService.sendMsg(topic,paymentInfoQuery.getOrderNo());
    }

    @Override
    public PaymentInfo savePaymentInfo(String paymentType, String orderNo,Long userId) {
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(userId);
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        paymentInfo.setOutTradeNo(UUID.randomUUID().toString().replaceAll("-",""));
        //  判断
        if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)){
            Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfo(orderNo);
            //  订单业务保存
            paymentInfo.setAmount(orderInfoResult.getData().getOrderAmount());
            paymentInfo.setContent("支付订单");
        } else {
            //  充值业务保存
            Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.getRechargeInfo(orderNo);
            paymentInfo.setAmount(rechargeInfoResult.getData().getRechargeAmount());
            paymentInfo.setContent("充值订单");

        }
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  paymentInfoMapper.insert();
        this.save(paymentInfo);
        return paymentInfo;
    }
}
